package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("strategies")
public class StrategiesController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/detail")
    public Object detail(String id) {
        Strategy strategy = strategyService.select(id);
        strategyStatisVORedisService.viewnumIncr(id);
        return JsonResult.success(strategy);
    }

    @GetMapping("/comments")
    public Object comments(int currentPage, String strategyId) {
        Page<StrategyComment> page = strategyCommentService.queryByStrategyId(currentPage, strategyId);
        return JsonResult.success(page);
    }

    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, HttpServletRequest request) {
        String token = request.getHeader("token");
        strategyCommentService.insert(strategyComment, token);
        strategyStatisVORedisService.replynumIncr(strategyComment.getStrategyId());
        return JsonResult.success();
    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, String sid, HttpServletRequest request) {
        String token = request.getHeader("token");
        strategyCommentService.commentThumb(cid, sid, token);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        StrategyStatisVO vo = strategyStatisVORedisService.get(sid);
        return JsonResult.success(vo);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userinfo) {
        boolean ret = strategyStatisVORedisService.thumbnumIncr(sid, userinfo.getId());
        Map<String, Boolean> map = new HashMap<>();
        map.put("ret", ret);
        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userinfo) {
        Map<String, Boolean> map = new HashMap<>();
        boolean flag = userInfoRedisService.sidFavor(sid, userinfo.getId());
        if (flag) {
            map.put("ret", false);
        } else {
            map.put("ret", true);
        }
        return JsonResult.success(map);
    }

    @GetMapping("/query")
    public Object query(@ModelAttribute("qo")StrategyQuery qo) {
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    @GetMapping("/themes")
    public Object themes() {
        List<StrategyTheme> strategyThemes = strategyThemeService.listAll();
        return JsonResult.success(strategyThemes);
    }
}
